package insert;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.connections;

public class edit_bene extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String oracleUser = "azienda";
    private String oraclePwd  = "azienda";
    private Connection conn;
    private String driver,oracle;
    private Statement st=null;
    private ResultSet rs=null;
	
    public edit_bene() {
        super();
    }
    
    public void init(ServletConfig sc) throws ServletException
    {
    	super.init(sc);
		driver = "oracle.jdbc.driver.OracleDriver";
		oracle = "jdbc:oracle:thin:@localhost:1521:xe";
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			conn = DriverManager.getConnection(oracle, oracleUser, oraclePwd);
			conn.setAutoCommit(false);
			st = conn.createStatement();
			rs = null;
	
			String importo 			= "" + request.getParameter("txtImporto");
			String data_acquisto 	= "" + request.getParameter("txtDataAcquisto");
			String garanzia 		= request.getParameter("chkGaranzia")==null?"0":"1";
			String data_attivazione = "" + request.getParameter("txtDataAttivazione");
			String data_scadenza	= "" + request.getParameter("txtDataScadenza");
			String conforme			= request.getParameter("chkConforme")==null?"0":"1";
			String obsoleto			= request.getParameter("chkObsoleto")==null?"0":"1";
			String subcategoria		= "" + request.getParameter("cmbSubcat");
			String fornitore		= "" + request.getParameter("cmbFor");
			String targhetta		= request.getParameter("chkTarghetta")==null?"0":"1";
			String descrizione		= connections.doubleQuota("" + request.getParameter("txtDescrizione"));
			String codice 			= "" + request.getParameter("txtCodice");
			String seriale 			= "" + request.getParameter("txtSeriale");
			String qry				= null;
			
			if (exists(codice))
			{
				qry = "update bene set "
								+ " numeroinventarioseriale  = '"+ seriale+"'"
								+ ",importo = " + importo 
								+ ",dataacquisto = to_date('"+data_acquisto+"','DD/MM/YYYY')"
								+ ",garanzia = '" + garanzia + "'"
								+ ",dataattivazione = to_date('"+data_attivazione+"','DD/MM/YYYY')"
								+ ",datascadenza = to_date('"+data_scadenza+"','DD/MM/YYYY')"
								+ ",conforme = '" + conforme + "'"
								+ ",obsoleto = '" + obsoleto + "'"
								+ ",sottocategoriabene = " + subcategoria
								+ ",fornitore = "+fornitore
								+ ",targhetta = '"+targhetta+"'"
								+ ",descrizione = '" +descrizione+"'"
								+ " where numeroinventariogenerico = " + codice;
				System.out.println(qry);
				st.executeUpdate(qry);
				conn.commit();
				System.out.println("UPDATE EFFETTUATO");
				response.sendRedirect("adm/ben/ins_bene.jsp?edit=1&pick="+codice);
				return;
			}
			System.out.println("ERRORE IN EXISTS");
			st.close();
			conn.close();
			response.sendRedirect("adm/ben/ins_bene.jsp?pick="+codice);
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private boolean exists(String code) throws SQLException, ClassNotFoundException
	{
		ResultSet rs;
		Statement st = connections.getStatement();
		String qry = "select count(*) as tot from bene where numeroinventariogenerico = " + code;
		int count=0;
		rs = st.executeQuery(qry);
		rs.next();
		count = rs.getInt("tot");
		rs.close();
		st.close();
		if (count==1)
			return true;
		else
			return false;
	}
}
